<template>
  <el-card class="box-card">
    <div slot="header" class="clearfix">
      <span>{{title}}</span>
    </div>
    <!-- 查询区域 -->
    <el-form :model="queryParams" :ref="queryFormRef" inline v-show="showSearch" label-width="120" @keyup.enter.native="handleQuery">
        <el-form-item label="关键字" prop="keywords" style="width: 220px;">
          <el-input v-model="queryParams.keywords" placeholder="请输入关键字" clearable style="width: 150px;"/>
        </el-form-item>
        <el-form-item label="状态" prop="status" label-width="40px" style="width: 170px;">
          <lam-dict-select v-model="queryParams.status" dictCode="status_type" placeholder="状态" clearable style="width: 120px;"/>
        </el-form-item>
        <el-form-item label-width="0px">
          <el-button type="primary" icon="el-icon-search"  @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh"  @click="resetQuery">重置</el-button>
        </el-form-item>
    </el-form>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="tool-bar-btn">
      <el-button type="primary" icon="el-icon-plus"  @click="handleAdd" v-hasPerms="['sys:dictItem:add']">新增</el-button>
      <el-button type="success" icon="el-icon-edit"  :disabled="single" @click="handleUpdate" v-hasPerms="['sys:dictItem:edit']">修改</el-button>
      <el-button type="danger" icon="el-icon-delete"  :disabled="multiple" @click="handleDelete" v-hasPerms="['sys:dictItem:remove']">删除</el-button>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </div>

    <selected-info :count="selectedRows.length" :onClearSelected="onClearSelected" />
    <el-table :ref="dataTableRef" v-loading="loading" :data="dataList" stripe border @selection-change="onSelectChange"
          :default-sort = "defaultSort" @sort-change="onSortChange" :row-key="rowKey" >
      <el-table-column type="selection" width="55" align="center" fixed="left"/>
      <el-table-column label="字典项值" align="center" prop="itemValue"  width="160" sortable="custom"/>
      <el-table-column label="字典项文本" align="center" prop="itemText"  width="160" sortable="custom"/>
      <el-table-column label="描述" align="center" prop="description" :show-overflow-tooltip="true" width="160" />
      <el-table-column label="排序" align="center" prop="sortOrder"  width="120" sortable="custom"/>
      <el-table-column label="状态" align="center" prop="statusDictText" width="80">
        <template slot-scope="{row}"><LamStatus :value="row.status" :text="row.statusDictText"/></template>
      </el-table-column>
      <el-table-column label="修改人" align="center" prop="updateBy"  width="120" />
      <el-table-column label="修改时间" align="center" prop="updateTime" width="180"/>
      <el-table-column label="操作" align="center" fixed="right" class-name="small-padding fixed-width" width="150">
        <template slot-scope="{row}">
          <el-button type="text" icon="el-icon-edit" @click="handleUpdate(row)" v-hasPerms="['sys:dictItem:edit']">修改</el-button>
          <el-button type="text" icon="el-icon-delete" @click="handleDelete(row)" v-hasPerms="['sys:dictItem:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="!loading" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" layout="total, sizes, prev, pager, next"/>

    <!-- 添加或修改对话框 -->
    <EditDictItem ref="formDialog" @ok="getList" />

  </el-card>

</template>

<script>
import {httpGet, httpDel} from '@/api';
import { IndexListMixin } from '@/components/mixins/IndexListMixin';
import EditDictItem from './edit';

export default {
  name: "DictItem",
  mixins:[IndexListMixin],
  components: {
    EditDictItem,
  },
  props: {
    dictId: {
      type: String,
      require: true
    },
    dictName:{
      type: String,
      require: true
    }
  },
  data() {
    return {
      //默认排序字段
      defaultOrderBy:'sortOrder',
      defaultOrderDirec:'ascending',
      // 查询参数
      queryParams: {
        dictId: null,
        keywords: null,
        status: null,
        pageNum: 1,
        pageSize: 10
      },
      url: {
        base: '/sys/dictItem/',
      	list: '/sys/dictItem/list'
      },
    };
  },
  computed:{
    title(){
      return this.$stringFormat("【{0}】的字典项", this.dictName);
    }
  },
  watch:{
    dictId(newVal){
      this.queryParams.dictId = newVal;
      if(newVal){
        this.getList();
      }else{
        this.dataList = [];
        this.total = 0;
      }
    },
  },
  created() {
  },
  methods: {
    getList() {
      this.loadData(this.url.list);
    },

    /** 新增按钮操作 */
    handleAdd() {
      this.$refs.formDialog.toAdd(this.$stringFormat("【{0}】添加字典项", this.dictName), {dictId: this.queryParams.dictId});
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      const id = row.id || this.selectedRowKeys
      this.$refs.formDialog.toUpdate(this.$stringFormat("【{0}】修改字典项", this.dictName), httpGet(this.url.base + id));
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.selectedRowKeys
      this.deleteData(this.url.base, ids);
    },

  }
};
</script>
